import requests
import re

url = 'http://maoyan.com/board/4'

for i in range(0, 10):
    # 数据和头文件准备
    data = {'offset': i * 10}
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
    }
    try:
        # 发送请求，抓取信息
        res = requests.get(url, params=data, headers=headers)
        # 解析数据
        html = res.content.decode('utf-8')
        # 正则表达式
        order = '''<i class="board-index .*>(.*?)</i>'''  # 名次
        name = '''<p class="name"><a .*>(.*?)</a></p>'''  # 电影名称
        star = '''<p class="star">\n\s*(.*?)\n\s*</p>'''  # 主演
        time = '''<p class="releasetime">(.*?)</p>\s*</div>'''  # 上映时间
        score = '''<i class="integer">(.*?)</i><i class="fraction">(.*?)</i>'''  # 得分
        img = '<img data-src="(.*?)" alt=".*" class="board-img" />'  # 图片地址
        # 处理正则
        orderlist = re.findall(order, html)
        namelist = re.findall(name, html)
        starlist = re.findall(star, html)
        timelist = re.findall(time, html)
        scorelist = re.findall(score, html)
        imglist = re.findall(img, html)
        # 输出显示
        for i in range(0, 10):
            print("{:10}|{:10}|{:10}|{:10}|{:1}{:1}|{}".format(
                orderlist[i], namelist[i], starlist[i], timelist[i], scorelist[i][0], scorelist[i][1], imglist[i]))
    except Exception as e:
        if hasattr(e, 'code'):
            print('HTTPError')
            print(e.reason)
            print(e.code)
        elif hasattr(e, 'reason'):
            print(e.reason)
        else:
            print(e)
